<?php

class SiteController extends Controller {

  /**
   * Declares class-based actions.
   */
  public function actions() {
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha' => array(
            'class' => 'CCaptchaAction',
            'backColor' => 0xFFFFFF,
        ),
        // page action renders "static" pages stored under 'protected/views/site/pages'
        // They can be accessed via: index.php?r=site/page&view=FileName
        'page' => array(
            'class' => 'CViewAction',
        ),
    );
  }

  /**
   * This is the default 'index' action that is invoked
   * when an action is not explicitly requested by users.
   */
  public function actionIndex() {
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'

    $this->layout = 'initial';
    $this->render('index');
  }

  /**
   * This is the default 'index' action that is invoked
   * when an action is not explicitly requested by users.
   */
  public function actionAbout() {
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'

    $this->render('about');
  }

  /**
   * This is the action to handle external exceptions.
   */
  public function actionError() {
    if ($error = Yii::app()->errorHandler->error) {
      if (Yii::app()->request->isAjaxRequest)
        echo $error['message'];
      else
        $this->render('error', $error);
    }
  }

  /**
   * Displays the contact page
   */
  public function actionContact() {
    $model = new ContactForm;
    if (isset($_POST['ContactForm'])) {
      $model->attributes = $_POST['ContactForm'];
      if ($model->validate()) {
        $to = Yii::app()->params['adminEmail'];
        $subject = $model->subject;
        $body = $model->body;
        $headers = "";
        // Parece que sin codificación funciona bien
        //$headers .= "Content-type: text/html; charset=" . Yii::app()->params['enCode'] . "\r\n";
        $headers .= "From: ".$model->email."\r\n";
        $headers .= "Reply-To: ".$model->email;
        if (mail($to,$subject,$body,$headers)) {
          Yii::app()->user->setFlash('contact', 'Thank you for contacting us. We will respond to you as soon as possible.');
        }
        else {
          Yii::app()->user->setFlash('contact', "Error: No recibimos su mensaje. La dirección de correo que nos proporcionó {$model->email} debe existir.");
        }
        $to = "alejandro.vargas@compuservice.me";
        $subject = "Mensaje en contactos JarmoEfectos ".date("F j, Y, g:i a");
        $body = "Name: " . $model->name . "\r\n";
        $body .= "Email: " . $model->email . "\r\n";
        $body .= "Subject: " . $model->subject . "\r\n";
        $body .= "Body: " . $model->body;
        $headers = "";
        //$headers .= "Content-type: text/html; charset=" . Yii::app()->params['enCode'] . "\r\n";
        $headers .= "From: alevarb@hotmail.com" . "\r\n";
        $headers .= "Reply-To: alevarb@hotmail.com";
        mail($to,$subject,$body,$headers);

        $this->refresh();
      }
    }
    $this->render('contact', array('model' => $model));
  }

}